layui.use(['table', 'admin', 'ax', 'func', 'upload'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    //文件上传
    var upload = layui.upload;

    //执行文件上传实例
    var uploadInst = upload.render({
        elem: '#importData' //绑定元素
        , url: Feng.ctxPath + '/sqInfo/importData_new' //上传接口
        , acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        , exts: 'xls|xlsx'
        , done: function (res) {
            // console.log(res)
            Feng.success("导入成功")
            table.reload(SqInfo.tableId);
        }
        , error: function () {
            //请求异常回调
        }
    });

    //下载导入模板
    $("#downloadModel").click(function () {
        window.location = Feng.ctxPath + '/data/downloadFile?id=' + 2
    })

    $("#btnDelete").click(function () {
        var checkRows = table.checkStatus(SqInfo.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请至少选择一条数据");
            return;
        }
        var operation = function () {
            var ids = new Array();
            for (var i = 0; i < checkRows.data.length; i++) {
                ids.push(checkRows.data[i].id)
            }
            // console.log(ids)
            $.ajax(Feng.ctxPath + '/sqInfo/allDelete', {
                data: {
                    ids: ids,
                },
                traditional: true,
                success: function (data) {
                    // console.log(data)
                    if (data == ids.length) {
                        Feng.success('删除成功')
                        table.reload(SqInfo.tableId)
                    }
                }
            })
        }
        Feng.confirm("确定是否删除?", operation);
    })

    /**
     * 社区表管理
     */
    var SqInfo = {
        tableId: "sqInfoTable"
    };

    /**
     * 初始化表格的列
     */
    SqInfo.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'isDel', sort: false, hide: true, title: '0正常 1删除'},
            {field: 'sqName', sort: false, title: '社区名称'},
            {field: 'sqAddr', sort: false, title: '社区地址'},
            {field: 'sjUnit', sort: false, title: '上级主管机关'},
            {field: 'lxPerson', sort: false, title: '联系人'},
            {field: 'lxPhone', sort: false, title: '联系电话'},
            {field: 'createPerson', sort: false, title: '创建人'},
            {field: 'createDate', sort: false, title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    SqInfo.search = function () {
        var queryData = {};

        queryData['id'] = $('#id').val();
        queryData['isDel'] = $('#isDel').val();
        queryData['sqName'] = $('#sqName').val();
        queryData['sqAddr'] = $('#sqAddr').val();
        queryData['sjUnit'] = $('#sjUnit').val();
        queryData['lxPerson'] = $('#lxPerson').val();
        queryData['lxPhone'] = $('#lxPhone').val();
        queryData['createPerson'] = $('#createPerson').val();
        queryData['createDate'] = $('#createDate').val();

        table.reload(SqInfo.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    SqInfo.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/sqInfo/add'
    };

    /**
     * 跳转到编辑页面
     *
     * @param data 点击按钮时候的行数据
     */
    SqInfo.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/sqInfo/edit?id=' + data.id
    };

    /**
     * 导出excel按钮
     */
    SqInfo.exportExcel = function () {
        var checkRows = table.checkStatus(SqInfo.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    SqInfo.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/sqInfo/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(SqInfo.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + SqInfo.tableId,
        url: Feng.ctxPath + '/sqInfo/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: SqInfo.initColumn(),
        skin: 'line',
        done: function (res, curr, count) {
            $('tr').css('background-color', '#ffffff');
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        SqInfo.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

        SqInfo.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        SqInfo.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + SqInfo.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            SqInfo.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            SqInfo.onDeleteItem(data);
        }
    });
});
